package cn.sinjet.phone;

import java.util.ArrayList;
import java.util.List;

import android.text.TextUtils;

public class ContactSearch {

/**
	 * 按号码-拼音搜索联系人
	 * 
	 * @param str
	 */
	public static ArrayList<Contact> search(final String str,
			final List<Contact> list) {
		ArrayList<Contact> contactList = new ArrayList<Contact>();
		if(list == null) return contactList;
		if(list.size() == 0) return contactList;
		// 如果搜索条件以0 1 +开头则按号码搜索
		if (str.startsWith("0") || str.startsWith("1") || str.startsWith("+")) {
			for (Contact contact : list) {
				if (contact.getNumber() != null && contact.getName() != null) {
					if (contact.getNumber().contains(str)
							|| contact.getName().contains(str)) {
						contact.setGroup(str);
						contactList.add(contact);
					}
				}
			}
			return contactList;
		}

		// final ChineseSpelling finder = ChineseSpelling.getInstance();
		// finder.setResource(str);
		// final String result = finder.getSpelling();
		// 先将输入的字符串转换为拼音
		// final String result = PinYinUtil.getFullSpell(str);
//		final String result = ChineseToEnglish.getPingYin(str);
//		Log.i("phone", "pinyin result:"+result+" str "+str);
		
		for (Contact contact : list) {
//			if (contains(contact, result)) {
			if(containsWithNumber(contact,str)){//edited by Kevin.Ye
				contactList.add(contact);
			}
		}

		return contactList;
	}
	
//edited by Kevin.Ye 	
public static boolean containsWithNumber(Contact contact,String search){
//	Log.i("phone", "contact:"+contact.getName()+" search:"+search);
	if (TextUtils.isEmpty(contact.getName()) || TextUtils.isEmpty(search)) {
		return false;
	}
	boolean flag = false;
	if (search.length() < 6) {//简拼搜索
		// String firstLetters = FirstLetterUtil.getFirstLetter(contact
		// .getName());
		// 获得首字母字符串
		//String firstLetters = UnicodeGBK2Alpha
			//	.getSimpleCharsOfString(contact.getName());
		// String firstLetters =
		// PinYinUtil.getFirstSpell(contact.getName());
//		Log.d("contact", "firstLetters:"+firstLetters);
//		firstLetters = strTo9Number(firstLetters);
//		Log.d("contact", "firstLetters to 9 num:"+firstLetters);
		// 不区分大小写
//		Pattern firstLetterMatcher = Pattern.compile("^" + search,
//				Pattern.CASE_INSENSITIVE);
//		flag = firstLetterMatcher.matcher(contact.getJianpinNumber()).find();
		flag = contact.getJianpinNumber().contains(search);
		/*Pattern firstLetterMatcher = Pattern.compile("^" + search,
				Pattern.CASE_INSENSITIVE);
		flag = firstLetterMatcher.matcher(contact.getJianpinNumber()).find();
		Log.i("phone", "quanpin match:"+flag);
		Log.i("phone", "quanpin contain:"+contact.getJianpinNumber().contains(search));*/
	}
	if(!flag){//全拼搜索
//		Pattern pattern2 = Pattern
//				.compile(search, Pattern.CASE_INSENSITIVE);
////		String pinyin = PinYin.getPinYin(contact.getName());
////		pinyin = strTo9Number(pinyin);
//		Matcher matcher2 = pattern2.matcher(contact.getQuanpinNumber());
//		flag = matcher2.find();
		flag = contact.getQuanpinNumber().contains(search);
	}
	return flag;
}

public static String strTo9Number(String string){
//	String result = "";
	StringBuilder sb = new StringBuilder();
	for(int i=0;i<string.length();i++){
		char c = string.charAt(i);
		if(c == 'a'||c == 'b'||c == 'c') sb.append("2");
		else if(c == 'd'||c == 'e'||c == 'f') sb.append("3");
		else if(c == 'g'||c == 'h'||c == 'i') sb.append("4");
		else if(c == 'j'||c == 'k'||c == 'l') sb.append("5");
		else if(c == 'm'||c == 'n'||c == 'o') sb.append("6");
		else if(c == 'p'||c == 'q'||c == 'r') sb.append("7");
		else if(c == 's'||c == 't'||c == 'u'||c == 'v') sb.append("8");
		else if(c == 'w'||c == 'x'||c == 'y'||c == 'z') sb.append("9");
	}
	return sb.toString();
}
	/**
	 * 根据拼音搜索
	 * 
	 * @param str
	 *            正则表达式
	 * @param pyName
	 *            拼音
	 * @param isIncludsive
	 *            搜索条件是否大于6个字符
	 * @return
	 */
	/*public static boolean contains(Contact contact, String search) {
		Log.i("phone", "contact:"+contact.getName()+" search"+search);
		if (TextUtils.isEmpty(contact.getName()) || TextUtils.isEmpty(search)) {
			return false;
		}

		boolean flag = false;

		// 简拼匹配,如果输入在字符串长度大于6就不按首字母匹配了
		if (search.length() < 6) {
			// String firstLetters = FirstLetterUtil.getFirstLetter(contact
			// .getName());
			// 获得首字母字符串
			String firstLetters = UnicodeGBK2Alpha
					.getSimpleCharsOfString(contact.getName());
			// String firstLetters =
			// PinYinUtil.getFirstSpell(contact.getName());
			// 不区分大小写
			Pattern firstLetterMatcher = Pattern.compile("^" + search,
					Pattern.CASE_INSENSITIVE);
			flag = firstLetterMatcher.matcher(firstLetters).find();
			Log.i("phone", "quanpin match:"+flag);
			Log.i("phone", "quanpin contain:"+firstLetters.contains(search));
		}

		if (!flag) { // 如果简拼已经找到了，就不使用全拼了
			// 全拼匹配
			// ChineseSpelling finder = ChineseSpelling.getInstance();
			// finder.setResource(contact.getName());
			// 不区分大小写
			Pattern pattern2 = Pattern
					.compile(search, Pattern.CASE_INSENSITIVE);
			Matcher matcher2 = pattern2.matcher(ChineseToEnglish.getPingYin(contact
					.getName()));
			flag = matcher2.find();
		}

		return flag;
	}*/
}
